package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.entity.Goal;
import com.util.DBHelp;
import com.util.RowMapper;

public class GoalDao {

	DBHelp<Goal> help=new DBHelp<Goal>();
	
	public List<Goal> getGoals(String pid,String eid) {
		String sql="SELECT id,`name`,`desc`,createtime,employeeid,projectid FROM t_goal WHERE projectid=? and employeeid=? ORDER BY `createtime` DESC";
		return help.executeQueryForList(sql, new Mapper(), pid,eid);
	}
	
	public void addGoal(Goal goal) {
		String sql="INSERT INTO t_goal(id,`name`,`desc`,createtime,employeeid,projectid) VALUES(?,?,?,?,?,?)";
		help.executeSQL(sql, goal.getId(),goal.getName(),goal.getDesc(),goal.getCreatetime(),goal.getEmployeeid(),goal.getProjectid());
			
	}
	
	private class Mapper implements RowMapper<Goal>{

		public Goal mapperRow(ResultSet rs) throws SQLException {
			Goal goal=new Goal();
			goal.setCreatetime(rs.getString("createtime"));
			goal.setDesc(rs.getString("desc"));
			goal.setEmployeeid(rs.getString("employeeid"));
			goal.setId(rs.getString("id"));
			goal.setName(rs.getString("name"));
			goal.setProjectid(rs.getString("projectid"));
			
			return goal;
		}
		
	}

	public Goal getGoal(String id) {
		String sql="SELECT id,`name`,`desc`,createtime,employeeid,projectid FROM t_goal WHERE id=?";
		return help.executeQueryForObject(sql, new Mapper(), id);
	}

	public void updateGoal(Goal goal) {
		String sql="UPDATE t_goal SET `name`=?,`desc`=?,createtime=?,employeeid=?,projectid=? WHERE id=?";
		help.executeSQL(sql, goal.getName(),goal.getDesc(),goal.getCreatetime(),goal.getEmployeeid(),goal.getProjectid(),goal.getId());
	}

	public List<Goal> getGoals(String id) {
		String sql="SELECT id,`name`,`desc`,createtime,employeeid,projectid FROM t_goal WHERE id IN(SELECT goalid FROM t_task WHERE employeeid=?)";
		return help.executeQueryForList(sql, new Mapper(), id);
	}

	public List<Goal> getAllByPid(String id) {
		String sql="SELECT `id`,`name`,`desc`,`createtime`,`employeeid`,`projectid` FROM `t_goal` WHERE `projectid`=? ORDER BY `createtime` DESC";
		return help.executeQueryForList(sql, new Mapper(), id);
	}

}
